<?php
namespace app\houtai\controller\user;
use app\houtai\controller\BaseController;
use think\db\Expression;
use think\Request;
class RewardController extends BaseController
{
    private $GAMETYPE = [1=>'房卡',2 => '金币', 3 => '混合'];
    private $GameType = ['RoomCardNum' => '房卡', 'ScoreNum' => '金币'];

    /**
     * @brief  : 奖励规则查询
     * @return : page
     * @details:
     */
    public function indexAction()
    {
        $conds = [];
        $order = [];
        $CHANNELS = $this->GetAdminChannel();
        $GAME_KIND = GetGameKind();
        ////////////////////////////////////////////////////////////////////////////
        $tt = $this->request->param('tt/d', 0); //tabletype
        $tb = $this->request->param('tb/d', 0);  //tablenumb
        $gt = $this->request->param('gt/d', -1);  //gametype
        if($tt > 0) $conds['a.TableType'] = $tt;
        if($tb > 0) $conds['a.TableNumb'] = $tb;
        if($gt >= 0){
            $conds['a.GameType'] = $gt;
        }

        $KindID = $this->request->param('KindID/d', 0);
        if ($KindID > 0 && array_key_exists($KindID, $GAME_KIND)) {
            $conds['a.KindID'] = $KindID;
        }
        $Channel = $this->request->param('Channel/d', -1);
        if ($Channel > 0 && array_key_exists($Channel, $CHANNELS)) {
            $conds['a.Channel'][] = ['IN', array_keys($CHANNELS)];
            $conds['a.Channel'][] = ['EQ', $Channel];
        } else {
            $conds['a.Channel'] = ['IN', array_keys($CHANNELS)];
        }
        $time1 = $this->request->param('time1/s', '', 'trim');
        $time2 = $this->request->param('time2/s', '', 'trim');

        $aa = YmdHis2Unix($time1);
        $bb = YmdHis2Unix($time2);
        $ta = date('Y-m-d 00:00:00', $aa);
        $tb = date('Y-m-d 23:59:59', $bb);
        if ($aa > 0) {
            if ($bb > 0) {
                if ($aa <= $bb) {
                    $conds['a.Addtime'] = ['BETWEEN TIME', [$ta, $tb]];
                } else {
                    $conds['a.Addtime'] = ['>= TIME', $ta];
                }
            } else {
                $conds['a.Addtime'] = ['>= TIME', $ta];
            }
        } else {
            if ($bb > 0) {
                $conds['a.Addtime'] = ['<= TIME', $tb];
            }
        }
        $search = $this->request->param('search/s', '', 'trim');
        if (!empty($search)) {
                $conds['a.str'] = ['LIKE', "%{$search}%"];
        }
        $ispass = $this->request->param('Sate/d', -1);
        if ($ispass >= 0 && in_array($ispass, [0, 1])) $conds['a.Sate'] = $ispass;

        $orderMap = [
          'A' => 'ID',
          'B' => 'Channel',
          'C' => 'KindID',
          'D' => 'GameType',
          'E' => 'TableType',
          'F' => 'TableNumb',
          'G' => 'Addtime',
          'H' => 'State',
          'I' => 'Addtime'
        ];
    $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'C';
    $order[$orderMap[$this->orderField]] = $this->orderDirection;
        $list = table("GameActivity A","rydb.basic")
                ->field(["a.ID","A.KindID","c.GameName","a.Addtime","a.Channel","d.Name","a.State","a.RoomCardNum","a.GameType","a.TableType","a.TableNumb","a.ScoreNum","a.str"])
                ->join("CYBasicDB.dbo.GameKindItem c","c.KindID = a.KindID","LEFT")
                ->join("CYBasicDB.dbo.ChannelConfig d","d.ID = a.Channel","LEFT")
                ->WHERE($conds)->order($order)->paginate();
        $tables = table("GameActivity","rydb.basic")->select();
        $TABLETYPE = array_unique(array_column($tables,"TableType"));
        $TABLENUMB = array_unique(array_column($tables,"TableNumb"));

        return $this->fetch('', [
            'time1' => $time1,
            'time2' => $time2,
            'search'=> $search,
            'sate'  => $ispass,
            'list'  => $list,
            'tt'  => $tt,
            'tb'  => $tb,
            'gt'  => $gt,
            'GAME_TYPE'  => $this->GAMETYPE,
            'channel'   => $Channel,
            'CHANNELS'  => $CHANNELS,
            'GAME_KIND' => $GAME_KIND,
            'TABLE_TYPE'=> $TABLETYPE,
            'TABLE_NUMB'=> $TABLENUMB,
            'KindID'    => $KindID,

        ]);
    }

    /**
     * @brief  : 奖励规则详情
     * @return : page|JSON
     * @details:
     */
    public function detailAction(){
        $CHANNELS = $this->GetAdminChannel();
        $GAME_KIND = GetGameKind();
//        var_dump($GAME_KIND);die;
        $id = $this->request->param('id/d', 0);
        if ($this->request->isGet()) { // 新建或读取
            $row = table("GameActivity A","rydb.basic")               
                ->where(['a.ID'=>$id])->find();
            return $this->fetch('', [
                'Channels' => $row['Channel'] ?? [],
                'gametypes' => $row['GameType'] ?? [],
                'kindid' => $row['KindID'] ?? [],
                'row' => $row ?? [],
                'GAME_TYPE' => $this->GAMETYPE,
                'KINDS' => $GAME_KIND,
                'CHANNEL' => $CHANNELS,
                'GameType' =>$this->GameType,

            ]);
        }else if($this->request->isPost()){
            $channel = $this->request->param('channel/d', 0);  //channel
            $KindID = $this->request->param('kind/d', 0);      //kindid
            $tt = $this->request->param('TableType/d', 0);     //tabletype
            $tb = $this->request->param('TableNumb/d', 0);     //tablenumb
            $gt = $this->request->param('gametype/d', 0);     //gametype
            $rc = $this->request->param('RoomCardNum/d', 0);     //roomcardnum
            $sc = $this->request->param('ScoreNum/d', 0);     //scorenum
            $str = $this->request->param('str/s','','trim');   //str
            $sate = $this->request->param('Sate/d',0);         //sate
//            return result(['a'=>$channel,'b'=>$KindID,'c'=>$tt,'d'=>$tb,'e'=>$gt,'f'=>$rc,'g'=>$sc,'k'=>$str,'s'=>$sate],101,"mm");
            if ($channel != 0 && array_key_exists($channel, $CHANNELS)) {   //判断渠道类型
                $Channel = $channel;
            } else {
                return result([],100,"未选择渠道");
            }

            if ($KindID > 0 && array_key_exists($KindID, $GAME_KIND)) {
                $conds['a.KindID'] = $KindID;
            }else{
                return result([],100,"未选择游戏");
            }

            if ($id>0){   //修改
                $tids = table("GameActivity","rydb.basic")->where(['TableNumb'=>$tb,'TableType'=>$tt,'GameType'=>$gt,'RoomCardNum'=>$rc,'ScoreNum'=>$sc,'KindID'=>$KindID,'Channel'=>$Channel])->value("ID");
                //判断是否存在
                if(!empty($tids)){
                    table("GameActivity","rydb.basic")->where(['ID'=>$tids])->update(['TableNumb'=>$tb,'TableType'=>$tt,'GameType'=>$gt,'RoomCardNum'=>$rc,'ScoreNum'=>$sc,'KindID'=>$KindID,'Channel'=>$Channel,'str'=>$str,'State'=>$sate]);
                    $tid = $tids;
                }else{
                    $tid = table("GameActivity","rydb.basic")->insertGetId(['TableNumb'=>$tb,'TableType'=>$tt,'GameType'=>$gt,'RoomCardNum'=>$rc,'ScoreNum'=>$sc,'KindID'=>$KindID,'Channel'=>$Channel,'str'=>$str,'State'=>$sate]);
                }
            
                if ($tid>0){
                    return result([], 0,'修改成功');
                }else{
                    return result([], 101,'修改失败');
                }

            }else{  //新增
                $cond = [];
                $cond['Channel'] = $Channel;
                $cond['KindID'] = $KindID;
                $cond['TableNumb'] = $tb;
                $cond['TableType'] = $tt;
                $cond['GameType'] = $gt;
                $cond['RoomCardNum'] = $rc;
                $cond['ScoreNum'] = $sc;
                $cond['str'] = $str;
                $cond['State'] =$sate;
                //是否存在相同规则的
                $ishave = table("GameActivity","rydb.basic")->where($cond)->value("ID");
                if ($ishave > 0){
                    return result([],101,"创建失败,该条规则已存在ID为:[{$ishave}]");
                }
                $tid = table("GameActivity","rydb.basic")->insertGetId($cond);
                if ($tid>0){
                    return result([], 0,'新增成功');
                }else{
                    return result([], 101,'新增失败');
                }
            }
        }
    }


    public function dellrulesAction(){
        if (!$this->request->isPost()) {
            return result([], 100, '非法请求');
        }
        $id = $this->request->param('id/d', 0);
        $ls = table("GameActivity","rydb.basic")->where(['ID'=>$id])->delete();
        return result([],0);
    }

}